package com.alili_tv.system.controller;

import com.alili_tv.system.common.R;
import com.alili_tv.system.entity.Tag;
import com.alili_tv.system.entity.VideoTag;
import com.alili_tv.system.service.TagService;
import com.alili_tv.system.service.VideoTagService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 标签表 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2023-03-17
 */
@RestController
@RequestMapping(value = "/tag", produces = "application/json")
public class TagController {

    @Resource
    private TagService tagService;

    @Resource
    private VideoTagService videoTagService;

    /**
     * 获取视频标签
     * @param videoId
     * @return
     */
    @GetMapping("/getByVideoId/{videoId}")
    public R<List<Tag>> getByVideoId(@PathVariable Long videoId) {
        // 获取视频标签基本信息
        // select * from video_tag where video_id = #{videoId}
        LambdaQueryWrapper<VideoTag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VideoTag::getVideoId, videoId);
        List<VideoTag> list = videoTagService.list(queryWrapper);

        // 获取标签名称
        List<Tag> collect = list.stream().map((item) -> {
            // select id, name from tag where id = #{id}
            LambdaQueryWrapper<Tag> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1
                    .select(Tag::getId, Tag::getName)
                    .eq(Tag::getId, item.getTagId());
            return tagService.getOne(queryWrapper1);
        }).toList();

        return R.success(collect);
    }


    /**
     * 获取推荐标签(通过分区id查询, 选取使用量前十)
     * @return
     */
    @GetMapping("/getByChannel/{channelId}")
    public R<List<Tag>> getByChannel(@PathVariable Integer channelId) {
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .select(Tag::getName)
                .eq(Tag::getChannelId, channelId)
                .orderByDesc(Tag::getChannelId)
                .last("limit 0, 10;");
        List<Tag> list = tagService.list(queryWrapper);
        return R.success(list);
    }
}
